package com.vcms.view.simulation.maintenance;

// Copyright 1997 ISS.
// The contents contained in this document may not be reproduced in any
// form or by any means, without the written permission of ISS, other
// than for the purpose for which it has been supplied.
//
// Author: Olivo Miotto, Pang Pingli
//

import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Dialog;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.Label;
import java.awt.Panel;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import com.vcms.controller.maintenance.MaintenanceController;
import com.vcms.controller.storemanagement.StoreController;
import com.vcms.exception.VMCSException;
import com.vcms.utility.formatter.MoneyFormatter;
import com.vcms.view.simulation.uicomponent.button.ButtonItem;
import com.vcms.view.simulation.uicomponent.display.CoinDisplay;
import com.vcms.view.simulation.uicomponent.display.DrinkDisplay;
import com.vcms.view.simulation.uicomponent.display.LabelledDisplay;
import com.vcms.view.simulation.uicomponent.listener.ExitButtonListener;
import com.vcms.view.simulation.uicomponent.listener.PasswordListener;
import com.vcms.view.simulation.uicomponent.listener.TotalCashButtonListener;
import com.vcms.view.simulation.uicomponent.listener.TransferCashButtonListener;
import com.vcms.view.simulation.uicomponent.listener.WarningDisplay;

@SuppressWarnings("serial")
public class MaintenancePanel extends Dialog {
	public final static char WORKING = 'a';
	public final static char PSWD = 'b';
	public final static char DIALOG = 'c';

	private static final String TITLE = "Maintenance Panel";
	private LabelledDisplay password;
	private LabelledDisplay collectCash;
	private Button exitBtn;
	private CoinDisplay cDisplay; // need to be access from other class.
	private DrinkDisplay dDisplay; // need to be access from other class.
	private ButtonItem totalCash;
	private Button transferCash;
	private WarningDisplay validPswd;
	private WarningDisplay invalidPswd;
	private MaintenanceController mctrl;

	public MaintenancePanel(Frame fr, MaintenanceController mc) {
		super(fr, TITLE, false);

		mctrl = mc;

		// north part
		Label lb = new Label(TITLE);
		lb.setFont(new Font("Helvetica", Font.BOLD, 24));
		Panel tp1 = new Panel();
		tp1.add(lb);

		Panel tpn = new Panel();
		tpn.setLayout(new GridLayout(0, 1));

		password = new LabelledDisplay("Password:", 30, LabelledDisplay.FLOW);
		password.setEchoChar('*');
		PasswordListener pl = new PasswordListener(mc.getAccessManager());
		password.addListener(pl);

		Panel tp3 = new Panel();
		validPswd = new WarningDisplay("Valid Password");
		invalidPswd = new WarningDisplay("Invalid Password");
		tp3.add(validPswd);
		tp3.add(invalidPswd);
		tpn.add(tp1);
		tpn.add((Panel)password.getLabelledDisplay());
		tpn.add(tp3);

		// center part
		Panel tpc = new Panel();
		tpc.setLayout(new GridLayout(0, 1));

		cDisplay = new CoinDisplay(mctrl);

		dDisplay = new DrinkDisplay(mctrl);

		Panel tp5 = new Panel();
		tp5.setLayout(new GridLayout(0, 1));

		// price = new LabelledDisplay("Brand Price", 5, LabelledDisplay.FLOW);
		totalCash = new ButtonItem("Show Total Cash Held", ButtonItem.FLOW);
		TotalCashButtonListener tl;

		tl = new TotalCashButtonListener(mctrl);
		totalCash.addListener(tl);

		transferCash = new Button("Press to Collect All Cash");
		transferCash.addActionListener(new TransferCashButtonListener(mctrl));

		Panel tp6 = new Panel();
		tp6.setLayout(new FlowLayout());
		tp6.add(transferCash);

		collectCash = new LabelledDisplay("Collect Cash:", 5,
				LabelledDisplay.FLOW);
		exitBtn = new Button("Press Here when Finished");
		exitBtn.addActionListener(new ExitButtonListener(mctrl));
		exitBtn.setFont(new Font("Helvetica", Font.BOLD, 12));
		tp5.add(totalCash);
		tp5.add(tp6);
		tp5.add((Panel)collectCash.getLabelledDisplay());

		Panel tpBtn = new Panel();
		tpBtn.add(exitBtn);

		tp5.add(tpBtn);
		tpc.setLayout(new BorderLayout());
		Panel pp = new Panel();
		pp.setLayout(new GridLayout(1, 2));
		pp.add(cDisplay);
		pp.add(dDisplay);
		// tpc.add(cDisplay);
		// tpc.add(dDisplay);
		tpc.add("Center", pp);
		tpc.add("South", tp5);

		this.setLayout(new BorderLayout());
		this.add("North", tpn);
		this.add("Center", tpc);

		pack();
		setLocation(200, 100);

		addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				if (mctrl.closeDown() == false)
					return;
				else
					mctrl.logoutMaintainer();

			}
		});

	}

	public void display() {
		this.setVisible(true);
	}

	public void closeDown() {
		dispose();

	}

	public CoinDisplay getCoinDisplay() {
		return cDisplay;
	}

	public DrinkDisplay getDrinksDisplay() {
		return dDisplay;
	}

	public void displayPasswordState(boolean st) {
		if (st == true) {
			validPswd.setState(true);
			invalidPswd.setState(false);
		} else {
			validPswd.setState(false);
			invalidPswd.setState(true);
		}

	}

	public void setActive(char comp, boolean st) {
		switch (comp) {
		case DIALOG:
			setActive(PSWD, st);
			setActive(WORKING, !st);
			validPswd.setState(false);
			invalidPswd.setState(false);
			break;
		case WORKING:
			collectCash.setActive(st);
			cDisplay.setActive(st);
			dDisplay.setActive(st);
			totalCash.setActive(st);
			transferCash.setEnabled(st);
			exitBtn.setEnabled(st);
			break;
		case PSWD:
			password.setActive(st);
			break;
		}
	}

	public int getCurIdx() {
		return dDisplay.getCurIdx();
	}

	public void displayTotalCash(int tc) {
		String stc;

		stc = MoneyFormatter.toMoney(tc);
		totalCash.setValue(stc);
	}

	public void displayCoins(int cc) {
		collectCash.setValue(cc);
	}

	/**
	 * use when machinery simulator panel changes qty; It is used to
	 * automatically update the displayed quantity in maintenance panel. It is
	 * called by Maintenance Controller Not required in requirement.
	 */

	public void updateQtyDisplay(char type, int idx, int qty)
			throws VMCSException {
		if (type == StoreController.CASH) {
			if (cDisplay.getCurIdx() == idx)
				cDisplay.displayQty(idx, qty);
		} else {
			if (dDisplay.getCurIdx() == idx)
				dDisplay.displayQty(idx, qty);
		}
	}

	/**
	 * When transfer all button is pushed, the current display needs to be
	 * updated. not required in requirement.
	 */
	public void updateCurrentQtyDisplay(char type, int qty)
			throws VMCSException {
		int curIdx;
		if (type == StoreController.CASH)
			curIdx = cDisplay.getCurIdx();
		else
			curIdx = dDisplay.getCurIdx();
		updateQtyDisplay(type, curIdx, qty);
	}

	public void initCollectCash() {
		collectCash.setValue("");
	}

	public void initTotalCash() {
		totalCash.setValue("");
	}

	public void clearPassword() {
		password.setValue("");
	}

	public void displayPrice(int price) {
		dDisplay.getPriceDisplay().setValue(MoneyFormatter.toMoney(price));
	}
}
